package com.xunua.mymycsjsdktest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import com.blankj.utilcode.util.ScreenUtils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;

import java.util.List;

public class TTNativeExpressDrawFeedAdActivity extends AppCompatActivity {
    private FrameLayout frameLayout;
    private static final String CODE_ID="901121041";
    private TTAdNative adNative;
    private TTNativeExpressAd ad;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_t_t_native_express_draw_feed_ad);
        initview();
        init();
    }

    private void init() {

        adNative = TTAdSdk.getAdManager().createAdNative(TTNativeExpressDrawFeedAdActivity.this);
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(CODE_ID)
                .setSupportDeepLink(true)
                .setAdCount(1)
                .setExpressViewAcceptedSize(ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight())
                .setImageAcceptedSize(640, 320)
                .build();
        adNative.loadExpressDrawFeedAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
                if (list==null||list.size()==0)
                    return;
                ad = list.get(0);
                //设置事件监听
                ad.setVideoAdListener(new TTNativeExpressAd.ExpressVideoAdListener() {
                    @Override
                    public void onVideoLoad() {

                    }

                    @Override
                    public void onVideoError(int i, int i1) {

                    }

                    @Override
                    public void onVideoAdStartPlay() {

                    }

                    @Override
                    public void onVideoAdPaused() {

                    }

                    @Override
                    public void onVideoAdContinuePlay() {

                    }

                    @Override
                    public void onProgressUpdate(long l, long l1) {

                    }

                    @Override
                    public void onVideoAdComplete() {

                    }

                    @Override
                    public void onClickRetry() {

                    }
                });
                //设置是否允许点击暂停视频播放
                ad.setCanInterruptVideoPlay(true);
                ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
                    @Override
                    public void onAdClicked(View view, int i) {

                    }

                    @Override
                    public void onAdShow(View view, int i) {

                    }

                    @Override
                    public void onRenderFail(View view, String s, int i) {

                    }

                    @Override
                    public void onRenderSuccess(View view, float v, float v1) {
                        frameLayout.removeAllViews();
                        frameLayout.addView(view);
                        ad.showInteractionExpressAd(TTNativeExpressDrawFeedAdActivity.this);
                    }
                });
                ad.render();
            }
        });


    }

    private void initview() {
        frameLayout=findViewById(R.id.framelayout_TTNativeExpressDrawFeedAd);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (ad!=null)
            ad.destroy();
    }
}